package evtop

import (
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/columnd"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/groupd"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/infod"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/interactiond"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
)

// Init 初始化事件监听
func Init(d ddd.Domain) {
	info := infoListener{
		base: base{
			d: d,
		},
		car: d.SubDomain(columnd.DomainID).AggregateRootByID(columnd.DomainID).(columnd.ColumnAggregateRoot),
		iar: d.SubDomain(infod.DomainID).AggregateRootByID(infod.DomainID).(infod.InfoAggregateRoot),
		gar: d.SubDomain(groupd.DomainID).AggregateRootByID(groupd.DomainID).(groupd.GroupAggregateRoot),
	}
	ddd.AddEventListener(infod.EvtTypInfo, infod.EvtActAdd, info.onInfoAdd())
	ddd.AddEventListener(infod.EvtTypInfo, infod.EvtActPublish, info.onInfoPublish())
	ddd.AddEventListener(infod.EvtTypInfo, infod.EvtActUnpublish, info.onInfoUnpublish())
	ddd.AddEventListener(infod.EvtTypInfo, infod.EvtActUpdate, info.onInfoDescChg())
	ddd.AddEventListener(infod.EvtTypInfo, infod.EvtActDelete, info.onInfoDelete())
	ddd.AddEventListener(infod.EvtTypInfo, infod.EvtActDelete, info.onInfoDeleteAutoDestroy())

	coll := newCollectingListener(d)
	ddd.AddEventListener(ddd.EvtTypeDDD, ddd.EvtActStartOK, coll.StartOk())
	ddd.AddEventListener(infod.EvtTypInfo, infod.EvtActAdd, coll.onInfoAdd())
	ddd.AddEventListener(infod.EvtTypInfo, infod.EvtActPublish, coll.onInfoPublish())
	ddd.AddEventListener(infod.EvtTypInfo, infod.EvtActUnpublish, coll.onInfoUnpublish())
	ddd.AddEventListener(infod.EvtTypInfo, infod.EvtActDelete, coll.onInfoDelete())
	ddd.AddEventListener(groupd.EvtTypGroup, groupd.EvtActStartCollecting, coll.onStartCollecting())
	ddd.AddEventListener(groupd.EvtTypGroup, groupd.EvtActStopCollecting, coll.onStopCollecting())

	interaction := newInteractionListener(d)
	ddd.AddEventListener(interactiond.EvtTypReplay, interactiond.EvtActReplay, interaction.onReplay())
	ddd.AddEventListener(interactiond.EvtTypInteraction, interactiond.EvtActAdd, interaction.onInteractionAdd())
	ddd.AddEventListener(interactiond.EvtTypInteraction, interactiond.EvtActDelete, interaction.onInteractionDelete())
	ddd.AddEventListener(interactiond.EvtTypInteraction, interactiond.EvtActUpdateValue, interaction.onInteractionUpdateValue())

	column := newColumnListener(d)
	ddd.AddEventListener(columnd.EvtTypColumn, columnd.EvtActUpdateCtl, column.onColumnUpdateCtlParam())
}
